from flask import Flask
from flask_sqlalchemy import SQLAlchemy

from sqlalchemy_utils import create_database, database_exists

db = SQLAlchemy()

def init_app(app: Flask):
    db.init_app(app)
    uri = app.config['SQLALCHEMY_DATABASE_URI']
    if not database_exists(uri):
        create_database(uri, encoding='utf8mb4')
    db.create_all()
    return db
